package com.zhang.struts.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

import com.zhang.pojo.Shop;
import com.zhang.pojo.User;
import com.zhang.struts.domain.UserForm;
import com.zhang.web.services.UserDAO;

public class LoginAction extends Action {
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            		throws Exception {
		String name = ((UserForm)form).getName();
		String password = ((UserForm)form).getPassword();
		User user = getUserDAO().isValid(name, password);

		
		if(user!=null){
			Shop shop = user.getShop();
			request.getSession().setAttribute("username", user.getName());
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("shop", shop);
			request.getSession().setAttribute("authority", user.getType());

			return mapping.findForward("success");
		}
		else
			return mapping.findForward("failure");
	}
}
